import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class FrameDemo2 extends WindowAdapter implements ActionListener{
	private Point lastLocation = null;
	private int maxX = 500;
	private int maxY = 500;
	
	private static JButton defaultButton=null;
	
	protected final static String NO_DECORATIONS = "no_dec";
	protected final static String LF_DECORATIONS = "laf_dec";
	protected final static String WS_DECORATIONS = "ws_dec";
	protected final static String CREATE_WINDOW = "new_win";
	protected final static String FILE_ICON = "file_icon";
	protected final static String DEFAULT_ICON = "def_icon";
	protected final static String PAINT_ICON = "paint_icon";
	protected final static String CC_ICON="colorchooser_icon";
	protected final static String DEF_WINDOW="default_color";
	protected final static String BLUE_WINDOW="blue_color";
	
	protected boolean noDecorations = false;
	protected boolean specifyIcon = false;
	protected boolean createIcon=false;
	protected boolean blueWindow= false;
	
	public FrameDemo2()
	{
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		maxX= screenSize.width - 50;
		maxY = screenSize.height - 50;
	}
	
	public void showNewWindow()
	{
		JFrame frame = new MyFrame();
		
		if(noDecorations)
			frame.setUndecorated(true);
		
		if(lastLocation!=null)
		{
			lastLocation.translate(40, 40);
			if((lastLocation.x>maxX) || (lastLocation.y>maxY))
				lastLocation.setLocation(0,0);
			frame.setLocation(lastLocation);
		}
		else
			lastLocation=frame.getLocation();
		
		if(specifyIcon)
		{
			if(createIcon)
				frame.setIconImage(createFDImage());
			else
				frame.setIconImage(getFDImage());
		}
		
		//if(blueWindow)
		//     frame.setForeground(new java.awt.Color(0,0,255));
		
		//frame.setForeground(Color.RED);
		//frame.repaint();
		if(blueWindow)
			frame.getContentPane().setBackground(Color.BLUE);
		
		frame.setSize(new Dimension(170,100));
		frame.setVisible(true);
	}
	
	protected JComponent createOptionControls()
	{
		JLabel label1 = new JLabel("Decoration options for subsequently created frames:");
		ButtonGroup bg1 = new ButtonGroup();
		JLabel label2 = new JLabel("Icon options:");
		ButtonGroup bg2 = new ButtonGroup();
		JLabel label3 = new JLabel("Frame color:");
		ButtonGroup bg3 = new ButtonGroup();
		
		JRadioButton rb1 = new JRadioButton();
		rb1.setText("Look and feel decorated");
		rb1.setActionCommand(LF_DECORATIONS);
		rb1.addActionListener(this);
		rb1.setSelected(true);
		bg1.add(rb1);
		
		JRadioButton rb2 = new JRadioButton();
		rb2.setText("Window system decorated");
		rb2.setActionCommand(WS_DECORATIONS);
		rb2.addActionListener(this);
		bg1.add(rb2);
		
		JRadioButton rb3 = new JRadioButton();
		rb3.setText("No decorations");
		rb3.setActionCommand(NO_DECORATIONS);
		rb3.addActionListener(this);
		bg1.add(rb3);
		
		JRadioButton rb4 = new JRadioButton();
		rb4.setText("Default icon");
		rb4.setActionCommand(DEFAULT_ICON);
		rb4.addActionListener(this);
		rb4.setSelected(true);
		bg2.add(rb4);
		
		JRadioButton rb5 = new JRadioButton();
		rb5.setText("Icon from a JPEG file");
		rb5.setActionCommand(FILE_ICON);
		rb5.addActionListener(this);
		bg2.add(rb5);
		
		JRadioButton rb6 = new JRadioButton();
		rb6.setText("Painted icon");
		rb6.setActionCommand(PAINT_ICON);
		rb6.addActionListener(this);
		bg2.add(rb6);
		
		JRadioButton rb7 = new JRadioButton();
		rb7.setText("Default");
		rb7.setActionCommand(DEF_WINDOW);
		rb7.addActionListener(this);
		rb7.setSelected(true);
		bg3.add(rb7);
		
		JRadioButton rb8 = new JRadioButton();
		rb8.setText("Blue");
		rb8.setActionCommand(BLUE_WINDOW);
		rb8.addActionListener(this);
		bg3.add(rb8);
		
		Box box = Box.createVerticalBox();
		box.add(label1);
		box.add(Box.createVerticalStrut(5));
		box.add(rb1);
		box.add(rb2);
		box.add(rb3);
		
		box.add(Box.createVerticalStrut(15));
		box.add(label2);
		box.add(Box.createVerticalStrut(5));
		box.add(rb4);
		box.add(rb5);
		box.add(rb6);
		
		box.add(Box.createVerticalStrut(15));
		box.add(label3);
		box.add(Box.createVerticalStrut(5));
		box.add(rb7);
		box.add(rb8);
		
		box.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		return box;
	}
	
	protected JComponent createButtonPane()
	{
		JButton button = new JButton("New window");
		button.setActionCommand(CREATE_WINDOW);
		button.addActionListener(this);
		defaultButton=button;
		
		JPanel pane = new JPanel();
		pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		pane.add(button);
		
		return pane;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		String command = e.getActionCommand();
		
		if(CREATE_WINDOW.equals(command))
			showNewWindow();
		else if(NO_DECORATIONS.equals(command))
		{
			noDecorations = true;
			JFrame.setDefaultLookAndFeelDecorated(false);
		}
		else if(WS_DECORATIONS.equals(command))
		{
			noDecorations=false;
			JFrame.setDefaultLookAndFeelDecorated(false);
		}
		else if(LF_DECORATIONS.equals(command))
		{
			noDecorations=false;
			JFrame.setDefaultLookAndFeelDecorated(true);
		}
		else if(DEFAULT_ICON.equals(command))
		{
			specifyIcon=false;
		}
		else if(FILE_ICON.equals(command))
		{
			specifyIcon=true;
			createIcon=false;
		}
		else if(PAINT_ICON.equals(command))
		{
			specifyIcon=true;
			createIcon=true;
		}
		else if(DEF_WINDOW.equals(command))
		{
			blueWindow=false;
		}
		else if(BLUE_WINDOW.equals(command))
		{
			blueWindow=true;
		}
	}
	
	protected static Image createFDImage()
	{
		BufferedImage bi = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
		
		Graphics g=bi.getGraphics();
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, 15, 15);
		g.setColor(Color.RED);
		g.fillOval(5,3,6,6);
		
		g.dispose();
		
		return bi;
	}
	
	protected static Image getFDImage()
	{
		java.net.URL imgURL = FrameDemo2.class.getResource("images/FD.jpg");
		if(imgURL!=null)
		{
			return new ImageIcon(imgURL).getImage();
		}
		else
		{
			return null;
		}
	}
	
	private static void createAndShowGUI()
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
		}catch(Exception e){}
		
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		
		JFrame frame = new JFrame("FrameDemo2");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		FrameDemo2 demo = new FrameDemo2();
		
		Container contentPane = frame.getContentPane();
		contentPane.add(demo.createOptionControls(), BorderLayout.CENTER);
		contentPane.add(demo.createButtonPane(), BorderLayout.PAGE_END);
		frame.getRootPane().setDefaultButton(defaultButton);
		
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	public static void main(String[] args)
	{
		javax.swing.SwingUtilities.invokeLater(new Runnable(){public void run(){createAndShowGUI();}});
	}
	
	class MyFrame extends JFrame implements ActionListener
	{
		public MyFrame()
		{
			super("A window");
			setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			
			JButton button = new JButton("Close window");
			button.addActionListener(this);
			
			Container contentPane = getContentPane();
			contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
			contentPane.add(Box.createVerticalGlue());
			contentPane.add(button);
			button.setAlignmentX(Component.CENTER_ALIGNMENT);
			contentPane.add(Box.createVerticalStrut(5));
		}
		
		public void actionPerformed(ActionEvent e)
		{
			setVisible(false);
			dispose();
		}
	}
}
